<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration 
{
	public function up()
	{
		Schema::create('articles', function(Blueprint $table) {
            $table->increments('id');
            $table->string('title')->index();
            $table->text('body');
            $table->bigInteger('user_id')->unsigned()->index();
            $table->integer('category_id')->unsigned()->index();
            $table->string('tags')->nullable();
            $table->integer('top')->unsigned()->default(0);
            $table->integer('reply_count')->unsigned()->default(0);
            $table->integer('view_count')->unsigned()->default(0);
            $table->integer('last_reply_user_id')->unsigned()->default(0);
            $table->integer('order')->unsigned()->default(0);
            $table->text('excerpt')->nullable();
            $table->string('slug')->nullable();
            $table->timestamps();
            //Articles表中category_id字段作为外键，与Category一对多关系
            $table->foreign('category_id')
                ->references('id')
                ->on('categories')
                ->onUpdate('cascade')
                ->onDelete('cascade');
            //Articles表中category_id字段作为外键，与Category一对多关系
            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onUpdate('cascade')
                ->onDelete('cascade');
        });
	}

	public function down()
	{
        //删除表时要删除外键约束，参数为外键名称
        Schema::table('articles', function(Blueprint $tabel){
            $tabel->dropForeign('articles_category_id_foreign');
            $tabel->dropForeign('articles_user_id_foreign');
        });
		Schema::drop('articles');
	}
}
